package cn.xmu.j2ee.exp.controller;

import cn.xmu.j2ee.exp.controller.vo.OrdersVo;
import cn.xmu.j2ee.exp.dao.bo.Orders;
import cn.xmu.j2ee.exp.service.OrdersService;
import cn.xmu.j2ee.exp.util.ReturnObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static cn.xmu.j2ee.exp.util.Common.cloneObj;

@RestController /*Restful的Controller对象*/
@RequestMapping(value = "/orders", produces = "application/json;charset=UTF-8")
public class OrdersController {
    private final Logger logger = LoggerFactory.getLogger(OrdersController.class);

    private final OrdersService ordersService;
    @Autowired
    public OrdersController(OrdersService ordersService) {
        this.ordersService = ordersService;
    }

    @GetMapping("{id}")
    public ReturnObject getOrdersById(@PathVariable("id") Long id, @RequestParam(name="with",defaultValue = "false")Boolean withOrderItemByDao) {
        logger.debug("getProductById: id = {} " ,id);
        Orders orders = null;
        orders = ordersService.findOrdersByID(id, withOrderItemByDao);
        OrdersVo ordersVo = cloneObj(orders, OrdersVo.class);
        logger.debug("getProductById: productVo = {} ", ordersVo );
        return new ReturnObject(ordersVo);
    }
}
